/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import org.joda.time.DateTime;
import org.xptracker.domain.Domain;
import org.xptracker.domain.EffortEntry;
import org.xptracker.domain.Story;
import org.xptracker.domain.TimeSegment;
import org.xptracker.services.reports.ChartBuildingReportRecipient;

import java.util.*;

class WebReportRecipient extends ChartBuildingReportRecipient {
  private final Date createdDate = new Date();
  private final Set<Story> existing = new LinkedHashSet<Story>();
  private final Map<Story, Date> added = new LinkedHashMap<Story, Date>();
  private final Map<Story, Date> removed = new LinkedHashMap<Story, Date>();
  private final Set<Story> completed = new LinkedHashSet<Story>();
  private final Map<String, List<EffortEntry>> effortEntries = new HashMap<String, List<EffortEntry>>();

  public WebReportRecipient(TimeSegment segment) {
    super(segment);
  }

  public void storyAdded(Domain domain, Date date, Story s, double amount) {
    super.storyAdded(domain, date, s, amount);
    storeEffort(domain, s);
    if (!segmentStartedOn(date)) {
      added.put(s, date);
    }
    existing.add(s);
    removed.remove(s);
    completed.remove(s);
  }

  public void storyRemoved(Domain domain, Date date, Story s, double amount) {
    super.storyRemoved(domain, date, s, amount);
    storeEffort(domain, s);
    removed.put(s, date);
    existing.remove(s);
    completed.remove(s);
  }

  public void storyCompleted(Domain domain, Date date, Story s, double amount) {
    super.storyCompleted(domain, date, s, amount);
    storeEffort(domain, s);
    completed.add(s);
    existing.remove(s);
    removed.remove(s);
  }

  private void storeEffort(Domain domain, Story s) {
    effortEntries.put(s.getId(), domain.listEffortEntries(s, segment.getStartDate(), segment.getStopDate()));
  }

  public boolean isExpired() {
    return new DateTime(createdDate).plusMinutes(15).isBefore(new DateTime());
  }

  public boolean describesSegment(TimeSegment segment) {
    return this.segment.equals(segment);
  }

  public Set<Story> getExisting() {
    return existing;
  }

  public Map<Story, Date> getAdded() {
    return added;
  }

  public Map<Story, Date> getRemoved() {
    return removed;
  }

  public Set<Story> getCompleted() {
    return completed;
  }

  public Map<String, List<EffortEntry>> getEffortEntries(Domain domain) {
    return effortEntries;
  }
}
